package com.example.explainus;

//Timer example
import java.util.Locale;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import com.example.units.GlobalParameters;
import com.example.units.GlobalVariables;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.preference.PreferenceActivity.Header;
import android.text.TextUtils.StringSplitter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class RoundActivity extends Activity 
{
   
	private Timer timer = new Timer();
	private boolean stopped = false;
	private TextView timerText;
	private TextView rightScore;
	private TextView wrongScore;
	private TextView tWord;
	private long starttime = 0;
	private boolean roundEnd = false;
	private Random rand;
	private int gTeam;
	private int score;
	private int wrong;
	
	Handler handler = new Handler();
    Runnable runnable = new Runnable() 
    {
        public void run() 
        {
        	long millis = System.currentTimeMillis() - starttime;
            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            seconds     = seconds % 60;
            timerText.setText(String.format("%d:%02d", minutes, seconds));
            handler.postDelayed(this, 500);
            if (seconds >= GlobalVariables.info.getRoundTime())
            {
            	timer.cancel();
            	handler.removeCallbacks(runnable);
            	GlobalVariables.info.setScore(gTeam, score);
            	Intent back = new Intent(RoundActivity.this, PrepareRoundActivity.class);
            	startActivity(back);
            	finish();
            }
            if (stopped)
            {
            	timer.cancel();
            	handler.removeCallbacks(runnable);
            }
        }
    };
	
    private void setRandomWord()
    {
    	rand = new Random();
    	int randInt = rand.nextInt(25) + 1;
    	tWord = (TextView)findViewById(R.id.tWord);
    	tWord.setText(Locale.getDefault().getDisplayLanguage());
        try
        {
        	Cursor c = GlobalVariables.baseUnits.selectWord(randInt%1000 + 1);
        	if (c != null)
        	{
        		tWord.setText(c.getString(c.getColumnIndex("word")));
        		Log.d("DEBUG_CURSOR", "C_NOT_NULL");
        	}
        	else
        	{
        		tWord.setText("NULL");
        	}
	       
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        }
    }
    
    public void onCreate(Bundle savedInstanceState) 
    {
    	score = 0;
    	wrong = 0;
        super.onCreate(savedInstanceState);
        stopped = false;
        setContentView(R.layout.activity_round);
        GlobalParameters.overrideFonts(this, findViewById(R.id.tWord));
        GlobalParameters.overrideFonts(this, findViewById(R.id.bRight));
        GlobalParameters.overrideFonts(this, findViewById(R.id.bWrong));
        GlobalParameters.overrideFonts(this, findViewById(R.id.tTimer));
        GlobalParameters.overrideFonts(this, findViewById(R.id.tScore));
        GlobalParameters.overrideFonts(this, findViewById(R.id.tMscore));
        GlobalParameters.overrideFonts(this, findViewById(R.id.bEndRound));
        gTeam = GlobalVariables.gameNow;
        timerText = (TextView)findViewById(R.id.tTimer);
    	rightScore = (TextView)findViewById(R.id.tScore);
    	wrongScore = (TextView)findViewById(R.id.tMscore);
    	wrongScore.setText(getResources().getString(R.string.tminusscore) + Integer.toString(wrong));
    	rightScore.setText(getResources().getString(R.string.tplusscore) + Integer.toString(score));
        starttime = System.currentTimeMillis();
        timer = new Timer();
        handler.postDelayed(runnable, 0);  
        setRandomWord();
    }
    
    public void goRight(View view)
    {
    	setRandomWord();
    	score = score + 1;
    	rightScore.setText(getResources().getString(R.string.tplusscore) + Integer.toString(score));
    }
    
    public void goWrong(View view)
    {
    	setRandomWord();
    	wrong = wrong + 1;
    	wrongScore.setText(getResources().getString(R.string.tminusscore) + Integer.toString(wrong));
    }
    
    public void goEndRound(View view)
    {
    	stopped = true;
    	GlobalVariables.info.eraseAll();
    	Intent back = new Intent(RoundActivity.this, NewGameActivity.class);
    	startActivity(back);
    	finish();
    }
}